package com.xbongbong.pro.statistics.controller;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.BaseDTO;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.statistic.pojo.dto.DataWarningDelDTO;
import com.xbongbong.pro.statistic.pojo.dto.DataWarningDetailsDTO;
import com.xbongbong.pro.statistic.pojo.dto.DataWarningEnableDTO;
import com.xbongbong.pro.statistic.pojo.dto.DataWarningGetDTO;
import com.xbongbong.pro.statistic.pojo.dto.DataWarningIndexDTO;
import com.xbongbong.pro.statistic.pojo.dto.DataWarningIndexListDTO;
import com.xbongbong.pro.statistic.pojo.dto.DataWarningListDTO;
import com.xbongbong.pro.statistic.pojo.dto.DataWarningListInChartDTO;
import com.xbongbong.pro.statistic.pojo.dto.DataWarningRemindDTO;
import com.xbongbong.pro.statistic.pojo.dto.DataWarningSaveDTO;
import com.xbongbong.pro.statistic.pojo.dto.DwExecutedRemindDTO;
import com.xbongbong.pro.statistic.pojo.vo.DataWarningDelVO;
import com.xbongbong.pro.statistic.pojo.vo.DataWarningEnableVO;
import com.xbongbong.pro.statistic.pojo.vo.DataWarningGetVO;
import com.xbongbong.pro.statistic.pojo.vo.DataWarningIndexListVO;
import com.xbongbong.pro.statistic.pojo.vo.DataWarningIndexVO;
import com.xbongbong.pro.statistic.pojo.vo.DataWarningListInChartVO;
import com.xbongbong.pro.statistic.pojo.vo.DataWarningListVO;
import com.xbongbong.pro.statistic.pojo.vo.DataWarningRemindVO;
import com.xbongbong.pro.statistic.pojo.vo.DataWarningSaveVO;
import com.xbongbong.pro.statistic.pojo.vo.DwExecutedRemindVO;
import com.xbongbong.pro.statistic.pojo.vo.DwHistoryListVO;
import com.xbongbong.pro.statistic.service.DataWarningService;
import com.xbongbong.pro.statistic.service.DataWarningStrategyService;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * @ClassName DataCenterController
 * @Description 数据中心
 * @Author mengna.Deng
 * @Date 2021/4/19 9:26
 * @Version 1.0
 */
@RestController
@RequestMapping(value = XbbProConstant.API_ROOT_PREFIX + "/chart/dataWarningSetting")
public class DataWarningController {
    @Resource
    private DataWarningService dataWarningService;
    @Resource
    private DataWarningStrategyService dataWarningStrategyService;

    /**
     * 获取设置
     *
     * @param getDTO 入参
     * @param br
     * @return
     * @throws
     * @author mengna.Deng
     * @date: 2021/4/19 9:26
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/get", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String get(@RequestBody @Valid DataWarningGetDTO getDTO, BindingResult br) {
        XbbResponse<DataWarningGetVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                DataWarningGetVO getVO = dataWarningService.addOrUpdateGet(getDTO);
                response = new XbbResponse<>(getVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * @ClassName DataCenterController
     * @Description 保存设置
     * @Author mengna.Deng
     * @Date 2021/4/20 16:07
     * @Version 1.0
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String save(@RequestBody @Valid DataWarningSaveDTO saveDTO, BindingResult br) {
        XbbResponse<DataWarningSaveVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                DataWarningSaveVO saveVO = dataWarningService.save(saveDTO);
                response = new XbbResponse<>(saveVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
    /**
     * @ClassName DataCenterController
     * @Description 启用/关闭数据预警
     * @Author mengna.Deng
     * @Date 2021/4/20 16:07
     * @Version 1.0
     */
    @RequestMapping(value = "/enable", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String enable(@RequestBody @Valid DataWarningEnableDTO enableDTO, BindingResult br) {
        XbbResponse<DataWarningEnableVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                DataWarningEnableVO enableVO = dataWarningService.enable(enableDTO);
                response = new XbbResponse<>(enableVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
    /**
     * @ClassName DataCenterController
     * @Description 删除数据预警
     * @Author mengna.Deng
     * @Date 2021/4/20 16:08
     * @Version 1.0
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String delete(@RequestBody @Valid DataWarningDelDTO dataWarningDelDTO, BindingResult br) {
        XbbResponse<DataWarningDelVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                DataWarningDelVO dataWarningDelVO = dataWarningService.delete(dataWarningDelDTO);
                response = new XbbResponse<>(dataWarningDelVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * @ClassName DataCenterController
     * @Description 图表中心获取数据预警列表
     * @Author mengna.Deng
     * @Date 2021/4/20 21:15
     * @Version 1.0
     */
    @RequestMapping(value = "/chartList", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String list4Chart(@RequestBody @Valid DataWarningListInChartDTO listDTO, BindingResult br) {
        XbbResponse<DataWarningListInChartVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                DataWarningListInChartVO listVO = dataWarningService.list4Chart(listDTO);
                response = new XbbResponse<>(listVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * @ClassName DataCenterController
     * @Description 数据中心获取数据预警列表
     * @Author mengna.Deng
     * @Date 2021/4/20 21:15
     * @Version 1.0
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String list(@RequestBody @Valid DataWarningListDTO listDTO, BindingResult br) {
        XbbResponse<DataWarningListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            //请求参数后处理，比如trim
            listDTO.postProcess();
            try {
                DataWarningListVO listVO = dataWarningService.list(listDTO);
                response = new XbbResponse<>(listVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
    /**
     * @ClassName DataCenterController
     * @Description 数据中心获取数据预警列表详情页
     * @Author mengna.Deng
     * @Date 2021/4/20 21:15
     * @Version 1.0
     */
    @RequestMapping(value = "/details", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String list(@RequestBody @Valid DataWarningDetailsDTO dataWarningDetailsDTO, BindingResult br) {
        XbbResponse<DwHistoryListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                DwHistoryListVO listVO = dataWarningService.warningDetails(dataWarningDetailsDTO);
                response = new XbbResponse<>(listVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * @ClassName DataCenterController
     * @Description 首页卡片
     * @Author mengna.Deng
     * @Date 2021/4/20 21:15
     * @Version 1.0
     */
    @RequestMapping(value = "/index/get", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String indexGet(@RequestBody @Valid DataWarningIndexDTO indexDTO, BindingResult br) {
        XbbResponse<DataWarningIndexVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                DataWarningIndexVO indexVO = dataWarningService.get(indexDTO);
                response = new XbbResponse<>(indexVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * @ClassName DataCenterController
     * @Description 首页卡片 查看全部
     * @Author mengna.Deng
     * @Date 2021/4/20 21:15
     * @Version 1.0
     */
    @RequestMapping(value = "/index/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String indexList(@RequestBody @Valid DataWarningIndexListDTO indexListDTO, BindingResult br) {
        XbbResponse<DataWarningIndexListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                //请求参数后处理，比如trim
                indexListDTO.postProcess();
                DataWarningIndexListVO indexListVO = dataWarningService.list(indexListDTO);
                response = new XbbResponse<>(indexListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * @ClassName DataCenterController
     * @Description 图表中心，预警可执行次数提醒
     * @Author mengna.Deng
     * @Date 2021/4/20 21:15
     * @Version 1.0
     */
    @RequestMapping(value = "/remind", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String remind(@RequestBody @Valid DataWarningRemindDTO remindDTO, BindingResult br) throws XbbException {
        XbbResponse<DataWarningRemindVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            DataWarningRemindVO remindVO = dataWarningService.remind(remindDTO);
            response = new XbbResponse<>(remindVO);
        }
        return JSON.toJSONString(response);
    }
    /**
     * @ClassName DataCenterController
     * @Description 数据中心，预警可执行次数提醒
     * @Author mengna.Deng
     * @Date 2021/4/20 21:15
     * @Version 1.0
     */
    @RequestMapping(value = "/executedRemind", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String executedRemind(@RequestBody @Valid DwExecutedRemindDTO remindDTO, BindingResult br) throws XbbException {
        XbbResponse<DwExecutedRemindVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            DwExecutedRemindVO remindVO = dataWarningService.executedRemind(remindDTO);
            response = new XbbResponse<>(remindVO);
        }
        return JSON.toJSONString(response);
    }
    @RequestMapping(value = "/addHistoryData", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String addHistoryData(@RequestBody @Valid BaseDTO baseDTO, BindingResult br) {
        XbbResponse<String> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            dataWarningStrategyService.dataWarningStrategy(baseDTO);
            response = new XbbResponse<>();
        }
        return JSON.toJSONString(response);
    }
}
